#region

using System;
using System.Linq;
using System.Web.Mvc;
using log4net;
using xConnected.Core.Interfaces.Service;
using xConnected.Core.Model;

#endregion

namespace xConnected.Controllers
{
    public class MessageExpertUserController : BaseController<MessageExpertUser>
    {
        protected IMessageExpertUserService MessageExpertUserService;
        protected IMessageService _messageService;
        protected IMessageFolderService _messageFolderService;
        protected IExpertProfileService _ExpertProfileService;
        protected IUserService _userService;

        public MessageExpertUserController(IMessageExpertUserService MessageExpertUserService,
                                             IMessageService messageService,
            IMessageFolderService messageFolderService,
                                             IExpertProfileService ExpertProfileService, IUserService userService)
        {
            base.Service = this.MessageExpertUserService = MessageExpertUserService;
            _messageService = messageService;
            _messageFolderService = messageFolderService;
            _ExpertProfileService = ExpertProfileService;
            _userService = userService;
        }

        [HttpPost]
        public JsonResult DeleteMesExpertUser(int messageExpertUserId)
        {
            ILog log = LogManager.GetLogger(GetType());
            try
            {
                var messageExpertUser = MessageExpertUserService.GetById(messageExpertUserId);
                var userId = _userService.GetUserByUsername(User.Identity.Name).Id;
                var deleteMesExpertUser =
                    _ExpertProfileService.GetById(messageExpertUser.ExpertProfileId).UserId == userId;
                var message = messageExpertUser.Message;
                var deleteMessage = message.MessageExpertUsers.Count == 1 && message.MessageCompanyUsers.Count == 0 &&
                                    message.MessagePositionUsers.Count == 0;
                if (deleteMesExpertUser)
                {
                    MessageExpertUserService.Delete(messageExpertUser);
                }

                if (deleteMessage)
                {
                    _messageService.Delete(message);
                }

                return Json("OK", JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message + Environment.NewLine + "Stack: " + ex.StackTrace, ex);
                return Json(new {Result = "ERROR", ex.Message}, JsonRequestBehavior.AllowGet);
            }
        }

        [HttpPost]
        public JsonResult MoveToTrash(int mesExpertUserId)
        {
            ILog log = LogManager.GetLogger(GetType());
            try
            {
                var userId = _userService.GetUserByUsername(User.Identity.Name).Id;
                var messageExpertUser = MessageExpertUserService.GetById(mesExpertUserId);
                messageExpertUser.MessageFolder =
                    _messageFolderService.GetAll().First(m => m.OwnerId == userId && m.FolderType == 2);
                MessageExpertUserService.SaveOrUpdate(messageExpertUser);

                return Json("OK", JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message + Environment.NewLine + "Stack: " + ex.StackTrace, ex);
                return Json(new {Result = "ERROR", ex.Message}, JsonRequestBehavior.AllowGet);
            }
        }
    }
}